package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * \* Description:用于删除购物车选中商品与全选购物车商品
 * \* @author: Lizhanpeng
 * \* Date: 2022/5/20
 * \
 */
@RestController
public class CarItemController {

    @Autowired
    ICartService cartService;

/**
 * @Description 全选购物车商品
 * @Param [checkAllItemRequest]
 * @Return com.cskaoyan.mall.commons.result.ResponseData
 * @Author Lizhanpeng
 * @Date 2022/5/20
 */
    @PutMapping("/shopping/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest checkAllItemRequest,HttpServletRequest request) {
        Long userId = parseUserInfo(request);
        checkAllItemRequest.setUserId(userId);

        CheckAllItemResponse checkAllItemResponse = cartService.checkAllCartItem(checkAllItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(checkAllItemResponse.getCode())){
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg("失败");
    }

/**
 * @Description 删除购物车中的商品
 * @Param [userId, pid]
 * @Return com.cskaoyan.mall.commons.result.ResponseData
 * @Author Lizhanpeng
 * @Date 2022/5/20
 */
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long userId,@PathVariable("pid") Long pid,HttpServletRequest request){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        Long userID = parseUserInfo(request);
        deleteCartItemRequest.setUserId(userId);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())){
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg("失败");
    }
    /**
     * @Description 删除已选择的商品
     * @Param [userId]
     * @Return com.cskaoyan.mall.commons.result.ResponseData
     * @Author Lizhanpeng
     * @Date 2022/5/20
     */


    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid") Long userId,HttpServletRequest request) {
        Long userID = parseUserInfo(request);
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(userId);
        DeleteCheckedItemResposne deleteCheckedItemResposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg("失败");
    }
    private Long parseUserInfo(HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.valueOf(object.get("uuid").toString());
        //Integer uId = Long.parseInt(object.get("uuid").toString());
        return userId;
    }



}
